import { roleList } from "@/apis/role"
import store from "@/store"
import bus from "@/util/bus"
import fileUpload from "@/components/file-upload.vue"
import { getOptions } from "@/apis/sales"
export default async (that)=>{
	let roleOption = []    
	roleOption = store.state.role.map(item=>({value:item.id,label:item.name}))
	
	// roleList().then(res=>{
	// 	console.log(res)
	// })
	getOptions().then(res=>{
		let options = [{label:"全部",value:0},...res.data]
		// res.data.unshift()
		store.commit("option/setSalesman",options)
	})
	return {
		columns:[
			{
				key:"id",
				label:"id",
			},
			{
				key:"nickname",
				label:"用户昵称",
			},
			// {
			// 	key:"openid",
			// 	label:"微信openid",
			// 	width:300
			// },
			// {
			// 	key:"avatarUrl",
			// 	label:"头像",
			// 	slot:"avatar",
			// 	width:80
			// },
			{
				key:"roleId",
				label:"用户身份",
				slot:"role",
				width:150
			},
			{
				key:"sid",
				label:"对应销售",
				slot:"sales"
			},
			{
				key:"createTime",
				label:"创建时间",
				width:200
			},
			{
				key:"updateTime",
				label:"修改时间",
				width:200
			},
			{
				key:"loginTime",
				label:"上次登录",
				width:200
			},
			{
				key:"operation",
				label:"操作",
				slot:"operation",
				width:250,
				props:{
					fixed:"right"
				},
				removeBtn:{
					show:true,
					permission:"user:delete"
				},
				editBtn:{
					show:true,
					permission:"user:edit"
				},

			}
		],
		reqOptions:{
			list:{
				method:"get",
				url:"/user",
			},
			insert:{
				method:"post",
				url:"/user"
			},
			update:{
				method:"put",
				url:"/user"
			},
			remove:{
				method:"delete",
				url:"/user/${id}"
			}
		},
		search:{
			form:{},
			options:[
				{
					key:"nickname",
					type:"el-input",
					label:"用户昵称",
					required:false,
					props:{
						clearable:true
					}
    
				},
				{
					key:"openid",
					type:"el-input",
					label:"openid",
					props:{
						clearable:true
					}
				},
                
				{
					key:"roleId",
					type:"el-select",
					label:"用户类型",
					required:false,
					options:roleOption,
					props:{
						clearable:true
					}
				},
				{
					key:"test",
					type:"el-button",
					label:"查询",
					removeLabel:true,
					width:false,
					props:{
						type:"primary"
					},
					listeners:{
						click(...e){
							that.searchFun && that.searchFun instanceof Function && that.searchFun(...e)
							that.$emit("search")
							console.log("click")
							that.getList()
						}
					}
				},
			],
			direction:"h",
			labelPosition:"right"
		},
		operation:{
			form:{},
			options:[
				{
					key:"btn1",
					type:"el-button",
					label:"添加用户",
					width:false,
					removeLabel:true,
					props:{
						type:"primary",
						icon:"el-icon-plus"
					},
					listeners:{
						click:(...e)=>{
							console.log(that)
							that.addFun && that.addFun instanceof Function && that.addFun(...e)
						}
					}
				},
				// {
				// 	key:"btn2",
				// 	type:"el-button",
				// 	label:"刷新",
				// 	width:false,
				// 	removeLabel:true,
				// 	props:{
				// 		type:"warning",
				// 		icon:"el-icon-refresh"
				// 	},
				// 	listeners:{
				// 		click:()=>{
				// 			that.getList()
				// 		}
				// 	}
				// }
			],
			direction:"h"
		},
		addField:async (form={})=>{
			return {
				form,
				direction:"h",
				options:[
					{
						key:"nickname",
						type:"el-input",
						label:"用户昵称",
						required:true,
						rules:[
							{ min: 1, max: 10, trigger: "blur" }
						]
					},
					{
						label:"密码",
						key:"pwd",
						type:"el-input",
						required:true,
						rules:[
							{min:4,max:10,trigger:"blur"}
						],
						props:{
							type:"password"
						}
					},
					// {
					// 	label:"头像",
					// 	key:"avatarUrl",
					// 	type:fileUpload,
					// 	props:{
					// 		actionType:"images/avatar",
					// 		imageField:"avatarUrl"
					// 	}
					// },
					{
						label:"角色",
						key:"roleId",
						required:true,
						type:"el-select",
						options:store.state.role.map(item=>({value:item.id,label:item.name}))
					},
					{
						label:"对应销售",
						key:"sid",
						type:"el-select",
						readStore:"salesman",
						// options
					}
				]
			}
		},
		updateField:async (form={})=>{
			return {
				form,
				direction:"h",
				options:[
					{
						key:"nickname",
						type:"el-input",
						label:"用户昵称",
						required:true,
						rules:[
							{ min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
						]
					},
					// {
					// 	label:"头像",
					// 	key:"avatarUrl",
					// 	type:fileUpload,
					// 	props:{
					// 		actionType:"images/avatar",
					// 		imageField:"avatarUrl",
					// 		multiple:false
					// 	}
					// },
					{
						label:"角色",
						key:"roleId",
						required:true,
						type:"el-select",
						options:roleOption
					},
					{
						label:"对应销售",
						key:"sid",
						type:"el-select",
						readStore:"salesman"
						// options
					}
				]
			}
		},
	}
}